<?php
/**
 * TbMallProduct.php class file.
 * @author wudg <544038230@qq.com>
 * @date 2020/11/16- 2:35 下午
 */

namespace common\dao;


use common\exception\ValidatorException;
use common\models\pdd\PddCopyProductsRefound;

class PddCopyProductsRefoundDao
{
    /**
     * 写入
     * @param array $data
     * @return PddMallProduct
     * @throws ValidatorException
     */
    public function insertData(array $data)
    {
        $mallProduct = new PddCopyProductsRefound();

        $mallProduct->load($data,'');

        if (!$mallProduct->validate()) {
            throw new ValidatorException($mallProduct->errorsToString());
        }

        if($mallProduct->save())
        {
            return $mallProduct;
        }else{
            throw new ValidatorException($mallProduct->errorsToOneString());
        }
    }


    /**
     * 更新商品
     * @param PddMallProduct $mallProduct
     * @param array $data
     * @return PddMallProduct
     * @throws ValidatorException
     */
    public function updateData(PddCopyProductsRefound $mallProduct, array $data)
    {
        $mallProduct->load($data,'');

        if (!$mallProduct->validate()) {
            throw new ValidatorException($mallProduct->errorsToString());
        }

        if($mallProduct->save())
        {
            return $mallProduct;
        }else{

            throw new ValidatorException($mallProduct->errorsToOneString());
        }
    }


    /**
     * 通过goods_id 获取商品详细信息
     * @param  $goods_id
     * @return PddMallProduct|null
     */
    public function getInfoByGoodsId($goods_id)
    {
        return PddCopyProductsRefound::findOne(['goods_id'=>$goods_id]);
    }

    /**
     * 通过主键获取信息
     * @param int $pk
     * @return PddCopyProducts|null
     */
    public function getInfoByPk(int $pk)
    {
        return PddCopyProductsRefound::findOne(['id' => $pk]);
    }



}
